package com.common.utils;

import java.text.DecimalFormat;

public class FileSizeUtil {
    public static final Long K_SIZE=1024L;
    public static final Long M_SIZE=1024*1024L;
    public static final Long G_SIZE=1024*1024*1024L;

    public static final String B="b";

    public static final String KB="Kb";

    public static final String MB="Mb";

    public static final String GB="Gb";

    public static String getFileSizeStr(long fileSize){
        String size="";
        if(fileSize==0L){
            return "0"+B;
        }
        //保留两位
        DecimalFormat format=new DecimalFormat("#0.00");
        if(fileSize<K_SIZE){
            size=format.format((double) fileSize)+B;
        }else if(fileSize<M_SIZE){
            size=format.format((double) fileSize/K_SIZE)+KB;
        }else if(fileSize<G_SIZE){
            size=format.format((double) fileSize/M_SIZE)+MB;
        }else {
            size=format.format((double) fileSize/G_SIZE)+GB;
        }
        return size;
    }
}
